package com.demo.logistics.stockControl.data;

import java.util.StringTokenizer;

public class DatabaseDescriptor {
	private static final String DB_NAME = "demologistics.db";
	private static final String STOCK_TBL = "stock_tbl";
	private static final String PRODUCT_TBL = "product_tbl";
	private static final String[][] STOCK_COLS = {
		{"prod_name", "text", "not null"},
		{"quantity", "integer", "not null"},
		{"lorry_plate", "text", "not null"},
		{"driver_name", "text", "not null"},
		{"location", "text", "null"}
	};
	private static final String[][] PRODUCT_COLS = {
		{"name", "text", "not null"},
		{"brand", "text", "not null"},
		{"price", "real", "not null"}
	};
	private static String STOCK_TBL_CREATE_STMT = makeCreateStatement(STOCK_TBL, STOCK_COLS, true); /*= "create table "+
	STOCK_TBL+
	" (id integer primary key autoincrement, "+
	"prod_name text not null, "+
	"quantity integer not null, "+
	"lorry_plate text not null, "+
	"driver_name text not null, "+
	"location text not null);";*/
	private static String PRODUCT_TBL_CREATE_STMT = makeCreateStatement(PRODUCT_TBL, PRODUCT_COLS, true); /*= "create table "+
	PRODUCT_TBL+
	" (id integer primary key autoincrement, "+
	"name text not null, "+
	"brand text not null, "+
	"price real not null);";*/
	private static String STOCK_TBL_DROP_STMT = makeDropStatement(STOCK_TBL);
	private static String PRODUCT_TBL_DROP_STMT = makeDropStatement(PRODUCT_TBL);
	
	public static String getStockTbl() {
		return STOCK_TBL;
	}
	public static String getProductTbl() {
		return PRODUCT_TBL;
	}
	public static String getProductTblCreateStmt() {
		return PRODUCT_TBL_CREATE_STMT;
	}
	public static String getStockTblCreateStmt() {
		return STOCK_TBL_CREATE_STMT;
	}
	public static String[] getAllCreateStmt(){
		String allStmt[] = {PRODUCT_TBL_CREATE_STMT, STOCK_TBL_CREATE_STMT};
		return allStmt;
	}
	public static String[] getAllDropStmt(){
		String allStmt[] = {PRODUCT_TBL_DROP_STMT, STOCK_TBL_DROP_STMT};
		return allStmt;
	}
	public static String getDbName() {
		return DB_NAME;
	}
	public static String makeCreateStatement(String tableName, String[][] cols, boolean addPrimaryKeyWithAI){
		String compStmt = "create table "+tableName+" (";
		if(addPrimaryKeyWithAI==true){
			compStmt += "id integer primary key autoincrement, ";
		}
		for(int i=0; i<cols.length; i++){
			if(i > 0){
				compStmt += ", ";
			}
			for(int j=0; j<cols[i].length; j++){
				if(j>0){
					compStmt += " ";
				}
				compStmt += cols[i][j];
			}
			
		}
		compStmt += ");";
		return compStmt;
	}
	
	public static String makeDropStatement(String tableName){
		String compStmt = "DROP TABLE IF EXIST "+tableName;
		return compStmt;
	}
	
	public static String getColumnName(String tableName, int colId, boolean includePrimaryKey){
		String theColName = null;
		if(includePrimaryKey==true){
			if(colId==0){
				return "id";
			}
			colId--;
		}
		String[][] tempColStruct = null;
		if(tableName.equals(PRODUCT_TBL)){
			tempColStruct = PRODUCT_COLS;
		}
		else if(tableName.equals(STOCK_TBL)){
			tempColStruct = STOCK_COLS;
		}
		if(tempColStruct!=null){
			for(int i=0; i<tempColStruct.length; i++){
				if(i==colId){
					theColName =  tempColStruct[i][0];
				}
			}
		}
		return theColName;
	}
	
	public static String getColumnType(String tableName, int colId, boolean includePrimaryKey){
		String theColType = null;
		if(includePrimaryKey==true){
			if(colId==0){
				return "integer";
			}
			colId--;
		}
		String[][] tempColStruct = null;
		if(tableName.equals(PRODUCT_TBL)){
			tempColStruct = PRODUCT_COLS;
		}
		else if(tableName.equals(STOCK_TBL)){
			tempColStruct = STOCK_COLS;
		}
		if(tempColStruct!=null){
			for(int i=0; i<tempColStruct.length; i++){
				if(i==colId){
					theColType =  tempColStruct[i][1];
				}
			}
		}
		return theColType;
	}
	
	public static String[] getAllColumnNames(String tableName){
		String[] noColNames = null;
		if(tableName.equals(PRODUCT_TBL)){
			int prodColLength = PRODUCT_COLS.length + 1;
			String[] allColNames = new String[prodColLength];
			for(int i=0; i<prodColLength; i++){
				allColNames[i] = getColumnName(tableName, i, true);
			}
			return allColNames;
		}
		else if(tableName.equals(STOCK_TBL)){
			int stockColLength = STOCK_COLS.length + 1;
			String[] allColNames = new String[stockColLength];
			for(int i=0; i<stockColLength; i++){
				allColNames[i] = getColumnName(tableName, i, true);
			}
			return allColNames;
		}
		return noColNames;
	}
}
